package org.come.Jpanel;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingUtilities;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.plaf.basic.BasicTreeUI;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreePath;

import org.come.until.CutButtonImage;
import org.come.until.RookieGuideMatterUntil;
import org.come.until.ScrollUI;
import org.come.until.SrcollPanelUI;

import com.tool.btn.FormsOnOffBtn;
import com.tool.tcpimg.RichLabel;
import com.tool.tcpimg.UIUtils;
import com.updateNew.MyIsif;



public class RookieGuideJpanel extends JPanel {

    private JScrollPane jScrollPane, // 玩法介绍滚动条
            jScrollPane2; // 说明 滚动条

    private JTree jTree; // 套装列表
    private DefaultMutableTreeNode top; // 总节点

    public RookieGuideJpanel() {
        if("水墨UI".equals(MyIsif.getStyle())) {
            this.setPreferredSize(new Dimension(579, 409));
            this.setLayout(null);
            this.setBackground(UIUtils.Color_BACK);
            // 关闭按钮事件
            FormsOnOffBtn offBtn = new FormsOnOffBtn("inkImg/old/2/1.png", 1, 68);
            offBtn.setBounds(579 - 37, 10, 25, 25);
            this.add(offBtn);
        }else{
            this.setPreferredSize(new Dimension(555, 407));
            this.setLayout(null);
            this.setBackground(UIUtils.Color_BACK);
            // 关闭按钮事件
            FormsOnOffBtn offBtn = new FormsOnOffBtn("inkImg/hongmu/1/s74.png", 1, 68);
            offBtn.setBounds(529, 0, 23, 23);
            this.add(offBtn);
        }

        top = new DefaultMutableTreeNode("");
        // 展示新手引导信息
        RookieGuideMatterUntil.showGuideMethod(top);

        // 套装列表
        jTree = new JTree(top);
        jTree.setOpaque(false);
        jTree.putClientProperty("JTree.lineStyle", "None"); // 去除jtree的线条
        ((BasicTreeUI) jTree.getUI()).setLeftChildIndent(0); // 设置父节点与子节点左对齐，并去除缩进
        DefaultTreeCellRenderer cellRenderer = new DefaultTreeCellRenderer() {

            Color color = new Color(237, 234, 0);
            Font font = new Font("微软雅黑", Font.BOLD, 15);

            @Override
            public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded,
                    boolean leaf, int row, boolean hasFocus) {
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
                super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);

                // 判断是几级的叶子
                if (node.getLevel() == 1) {
                    setForeground(color);
                    setFont(font);
                }
                if (node.getLevel() == 2) {
                    setFont(UIUtils.TEXT_FONT1);
                }
                return this;
            }
        };
        cellRenderer.setLeafIcon(null); // 设置叶子图标
        try {
            cellRenderer.setOpenIcon(CutButtonImage.cuts("skin/8100226/button/B108.png")[0]);// 设置打开子节点图标
            cellRenderer.setClosedIcon(CutButtonImage.cuts("skin/8100226/button/B109.png")[0]); // 设置关闭子节点图标
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        // cellRenderer.setFont(new Font("宋体", Font.BOLD, 16));// 设置字体.
        cellRenderer.setBackgroundNonSelectionColor(UIUtils.Color_BACK);// 设置非选定节点的背景色
        cellRenderer.setBackgroundSelectionColor(new Color(99, 93, 90));// 设置节点在选中状态下的背景色
        cellRenderer.setBorderSelectionColor(UIUtils.Color_BACK);// 设置选中状态下节点边框的颜色

        /*
         * 设置选时或不选时，文字的变化颜色
         */
        cellRenderer.setTextNonSelectionColor(Color.white);// 设置绘制未选中状态下节点文本的颜色
        cellRenderer.setTextSelectionColor(Color.white);// 设置绘制选中状态下节点文本的颜色
        jTree.setCellRenderer(cellRenderer);
        jTree.setRootVisible(false); // 隐藏根节点
        jTree.setRowHeight(25); // 设置节点行高
        // 单击展开子节点
        jTree.addMouseListener(new MouseAdapter() { // 添加鼠标事件处理
            @Override
            public void mousePressed(MouseEvent e) {
                if (SwingUtilities.isLeftMouseButton(e)) { // 点击了鼠标左键
                    TreePath path = jTree.getSelectionPath();
                    if (path != null) {
                        if (jTree.isExpanded(path)) {
                            jTree.collapsePath(path);// 关闭节点
                        } else {
                            jTree.expandPath(path);// 展开节点
                        }
                    }

                }
            }
        });
        jTree.addTreeSelectionListener(new TreeSelectionListener() {

            @Override
            public void valueChanged(TreeSelectionEvent e) {
                JTree tree = (JTree) e.getSource();
                // 利用JTree的getLastSelectedPathComponent()方法取得目前选取的节点.
                DefaultMutableTreeNode selectionNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();

                if (selectionNode == null) {
                    jScrollPane2.setViewportView(null);
                    return;
                }
                String nodeName = selectionNode.toString();
                if ("游戏介绍".equals(nodeName) || "充值比例".equals(nodeName) || "版本特色".equals(nodeName)
                        || "功能介绍".equals(nodeName)) {
                    jScrollPane2.setViewportView(null);
                    if (RookieGuideMatterUntil.returnContent(nodeName) == null) {
                        return;
                    }
                    RichLabel label = new RichLabel(RookieGuideMatterUntil.returnContent(nodeName), UIUtils.TEXT_FONT);
                    Dimension d = label.computeSize(250);
                    label.setSize(d);
                    label.setPreferredSize(d);
                    jScrollPane2.setViewportView(label);
                    return;
                }
                if (selectionNode.isLeaf()) {// 判断是否是叶子节点
                    jScrollPane2.setViewportView(null);
                    if (RookieGuideMatterUntil.returnChildMsg(nodeName) == null) {
                        return;
                    }
                    RichLabel label = new RichLabel(RookieGuideMatterUntil.returnChildMsg(nodeName), UIUtils.TEXT_FONT);
                    Dimension d = label.computeSize(250);
                    label.setSize(d);
                    label.setPreferredSize(d);
                    jScrollPane2.setViewportView(label);
                } else {
                    jScrollPane2.setViewportView(null);
                    if (RookieGuideMatterUntil.returnContent(nodeName) == null) {
                        return;
                    }
                    RichLabel label = new RichLabel(RookieGuideMatterUntil.returnContent(nodeName), UIUtils.TEXT_FONT);
                    Dimension d = label.computeSize(250);
                    label.setSize(d);
                    label.setPreferredSize(d);
                    jScrollPane2.setViewportView(label);
                }
            }
        });

        // 套装列表滚动条
        jScrollPane = new JScrollPane(jTree);
        jScrollPane.setVerticalScrollBarPolicy(22);
        jScrollPane.getVerticalScrollBar().setUI(new SrcollPanelUI());
        jScrollPane.getViewport().setOpaque(false);
        jScrollPane.setOpaque(false);
        if("水墨UI".equals(MyIsif.getStyle())) {
            jScrollPane.setBounds(48, 79, 217, 290);
        }else{
            jScrollPane.setBounds(26, 60, 209, 315);
        }
        jScrollPane.setBorder(BorderFactory.createEmptyBorder());
        jScrollPane.setHorizontalScrollBarPolicy(31);
        this.add(jScrollPane);

        // 套装列表滚动条
        jScrollPane2 = new JScrollPane();
        jScrollPane2.setVerticalScrollBarPolicy(22);
        jScrollPane2.getVerticalScrollBar().setUI(new SrcollPanelUI());
        jScrollPane2.getVerticalScrollBar().setUnitIncrement(50);
        jScrollPane2.getViewport().setOpaque(false);
        jScrollPane2.setOpaque(false);
        if("水墨UI".equals(MyIsif.getStyle())) {
            jScrollPane2.setBounds(272, 79, 288, 290);
        }else{
            jScrollPane2.setBounds(255, 60, 275, 315);
        }
        jScrollPane2.setBorder(BorderFactory.createEmptyBorder());
        jScrollPane2.setHorizontalScrollBarPolicy(31);
        this.add(jScrollPane2);
    }

    private ImageIcon icon_hm = new ImageIcon("inkImg/hongmu/2/rookieguide.png");

    private ImageIcon icon_sm = new ImageIcon("inkImg/newbackground/B231.png");

    @Override
    protected void paintComponent(Graphics g) {
        // TODO Auto-generated method stub
        super.paintComponent(g);
        if("水墨UI".equals(MyIsif.getStyle())) {
            g.drawImage(icon_sm.getImage(), 0, 0, 579, 409, this);
        }else{
            g.drawImage(icon_hm.getImage(), 0, 0, 555, 407, this);
        }

    }
}
